#include <bits/stdc++.h>
using namespace std;
long long n, a[1007], c[1007], sum, avg, ans;

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	avg = sum / n;
	for (int i = 1; i <= n; i++)
	{
		c[i] = c[i - 1] + avg - a[i];
	}
	sort(c + 1, c + n + 1);
	for (int i = 1; i <= n; i++)
		ans += abs(c[n / 2 + 1] - c[i]);
	cout << ans;
	return 0;
}